﻿using System;
using System.Xml.Linq;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x0200005F RID: 95
    public static class PosbarPayProxy {
        // Token: 0x06000315 RID: 789 RVA: 0x0000E6DC File Offset: 0x0000C8DC
        public static void AddPosbarPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("GET/handler/RouteHandler.ashx", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/handler/RouteHandler.ashx", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/pay/Leshua.ashx", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000316 RID: 790 RVA: 0x00003F50 File Offset: 0x00002150
        private static void Init(HttpContext ctx) {
            ctx.Result = new HttpHtmlResult(string.Format("OK http://{0}:{1}", ctx.Connection.LocalEndPoint.Address, ctx.Request.Url.Port));
        }

        // Token: 0x06000317 RID: 791 RVA: 0x0000E740 File Offset: 0x0000C940
        private static void Process(HttpContext ctx) {
            PosbarPayResult posbarPayResult = new PosbarPayResult();
            try {
                string text = ctx.Request.Form["ActionCmd"];
                posbarPayResult.ResultVal.ClientTradeNo = ctx.Request.Form["cuid"];
                HongshPayTrade hongshPayTrade;
                if (text.Equals("BarCode", StringComparison.OrdinalIgnoreCase)) {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Authcode = ctx.Request.Form["dynamicId"],
                        Subject = ctx.Request.Form["subject"],
                        Amount = (int)Math.Ceiling(HongshExtends.ConvertTo<decimal>(ctx.Request.Form["total"]) * 100m)
                    });
                    posbarPayResult.ResultCode = 1;
                }
                else {
                    if (!text.Equals("Query2", StringComparison.OrdinalIgnoreCase)) {
                        throw new Exception("不支持该接口");
                    }
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        TradeId = ctx.Request.Form["out"]
                    });
                }
                if (hongshPayTrade.State == TradeState.失败) {
                    throw new Exception("支付超时或失败，请撤销订单！");
                }
                posbarPayResult.ResultVal.Out_trade_no = hongshPayTrade.TradeId;
                if (hongshPayTrade.State == TradeState.成功) {
                    posbarPayResult.ResultCode = 1;
                    posbarPayResult.ResultMsg = posbarPayResult.ResultVal.Message = "支付成功";
                    posbarPayResult.ResultVal.IsPaySuccess = "y";
                    posbarPayResult.ResultVal.Total_fee = Math.Round(hongshPayTrade.Amount / 100m, 2);
                    posbarPayResult.ResultVal.Receipt_Amount = Math.Round(hongshPayTrade.Amount / 100m, 2);
                }
                else {
                    posbarPayResult.ResultMsg = "等待支付中 - " + ctx.Request.Form["dogno"];
                }
            }
            catch (Exception ex) {
                posbarPayResult.ResultCode = 0;
                posbarPayResult.ResultMsg = ex.Message;
            }
            ctx.Result = new HttpHtmlResult("<?xml version=\"1.0\" encoding=\"utf - 8\"?>" + HongshExtends.ToXElement(posbarPayResult, "Pospay").ToString(SaveOptions.DisableFormatting));
        }

        // Token: 0x02000060 RID: 96
        public class PosbarPayPaymentResult {
            // Token: 0x17000155 RID: 341
            // (get) Token: 0x06000318 RID: 792 RVA: 0x00003F8C File Offset: 0x0000218C
            // (set) Token: 0x06000319 RID: 793 RVA: 0x00003F94 File Offset: 0x00002194
            public string ClientTradeNo { get; set; }

            // Token: 0x17000156 RID: 342
            // (get) Token: 0x0600031A RID: 794 RVA: 0x00003F9D File Offset: 0x0000219D
            // (set) Token: 0x0600031B RID: 795 RVA: 0x00003FA5 File Offset: 0x000021A5
            public string Out_trade_no { get; set; }

            // Token: 0x17000157 RID: 343
            // (get) Token: 0x0600031C RID: 796 RVA: 0x00003FAE File Offset: 0x000021AE
            public string Trade_no {
                get {
                    return Out_trade_no;
                }
            }

            // Token: 0x17000158 RID: 344
            // (get) Token: 0x0600031D RID: 797 RVA: 0x00003FB6 File Offset: 0x000021B6
            // (set) Token: 0x0600031E RID: 798 RVA: 0x00003FBE File Offset: 0x000021BE
            public string Message { get; set; }

            // Token: 0x17000159 RID: 345
            // (get) Token: 0x0600031F RID: 799 RVA: 0x00003FC7 File Offset: 0x000021C7
            // (set) Token: 0x06000320 RID: 800 RVA: 0x00003FCF File Offset: 0x000021CF
            public decimal Total_fee { get; set; }

            // Token: 0x1700015A RID: 346
            // (get) Token: 0x06000321 RID: 801 RVA: 0x00003FD8 File Offset: 0x000021D8
            // (set) Token: 0x06000322 RID: 802 RVA: 0x00003FE0 File Offset: 0x000021E0
            public decimal Receipt_Amount { get; set; }

            // Token: 0x1700015B RID: 347
            // (get) Token: 0x06000323 RID: 803 RVA: 0x00003FE9 File Offset: 0x000021E9
            // (set) Token: 0x06000324 RID: 804 RVA: 0x00003FF1 File Offset: 0x000021F1
            public string IsPaySuccess { get; set; }

            // Token: 0x06000325 RID: 805 RVA: 0x00003FFA File Offset: 0x000021FA
            public PosbarPayPaymentResult() {
                // Class15.cwcZ3SWztHroi();
                IsPaySuccess = "n";
                // base..ctor();
            }
        }

        // Token: 0x02000061 RID: 97
        public class PosbarPayResult {
            // Token: 0x1700015C RID: 348
            // (get) Token: 0x06000326 RID: 806 RVA: 0x00004012 File Offset: 0x00002212
            // (set) Token: 0x06000327 RID: 807 RVA: 0x0000401A File Offset: 0x0000221A
            public int ResultCode { get; set; }

            // Token: 0x1700015D RID: 349
            // (get) Token: 0x06000328 RID: 808 RVA: 0x00004023 File Offset: 0x00002223
            // (set) Token: 0x06000329 RID: 809 RVA: 0x0000402B File Offset: 0x0000222B
            public string ResultMsg { get; set; }

            // Token: 0x1700015E RID: 350
            // (get) Token: 0x0600032A RID: 810 RVA: 0x00004034 File Offset: 0x00002234
            public PosbarPayPaymentResult ResultVal { get; }

            // Token: 0x0600032B RID: 811 RVA: 0x0000403C File Offset: 0x0000223C
            public PosbarPayResult() {
               // Class15.cwcZ3SWztHroi();
                ResultVal = new PosbarPayPaymentResult();
               // base..ctor();
            }
        }
    }
}
